<?php
namespace app\tadmin\model;
use app\common\model\Category as CategoryModel;

use Overtrue\Pinyin\Pinyin;
/**
 * 文档分类
 */
class Category extends CategoryModel {

    public function add($data){
        $pinyin = new Pinyin();
        $pinyinArr = $pinyin->convert($data['title']);
        if(count($pinyinArr) > 0){
            $data['pinyin'] = implode('' , $pinyinArr);
        }else{
            $data['pinyin'] = 'ch'.rand(100,999);
        }
        return $this->allowField(true)->save($data);
    }

    public function edit($data){
        $pinyin = new Pinyin();
        $pinyinStr = $pinyin->convert($data['title']);
        $data['pinyin'] = implode('' , $pinyinStr);
        return $this->allowField(true)->save($data);
    }
    
    /**
     * 生成树
     */
    public static function getTree($types){
        $data = self::getAll()->toArray();
        $data = list_sort_by($data , 'sort_order');
        foreach($data as $k => $v){
            $data[$k]['type_str'] = isset($types[$v['type']]) ? $types[$v['type']] : '未选择';
        }
        return list_to_tree($data);
    }
}